package com.gzc.just.play.last.war.common.share.service;

import com.gzc.just.play.last.war.common.share.data.DataAccessService;
import com.gzc.just.play.last.war.common.share.cache.UnifiedCacheService;
import com.gzc.just.play.last.war.common.share.config.AllInOneConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 数据访问管理器
 * 统一管理数据访问和缓存服务，提供便捷的API
 */
@Service
public class DataAccessManager {
    
    private static final Logger logger = LoggerFactory.getLogger(DataAccessManager.class);
    
    private final DataAccessService dataAccessService;
    private final UnifiedCacheService cacheService;
    private final AllInOneConfig.EnvironmentDetector environmentDetector;
    
    @Autowired
    public DataAccessManager(DataAccessService dataAccessService, 
                           UnifiedCacheService cacheService,
                           AllInOneConfig.EnvironmentDetector environmentDetector) {
        this.dataAccessService = dataAccessService;
        this.cacheService = cacheService;
        this.environmentDetector = environmentDetector;
        
        logger.info("Data access manager initialized successfully");
        logger.info("Current environment: {}", environmentDetector.getActiveProfile());
        logger.info("Data access service type: {}", dataAccessService.getDatabaseType());
        logger.info("Cache service type: {}", cacheService.getCacheType());
    }
    
    /**
     * 获取数据访问服务
     */
    public DataAccessService getDataAccessService() {
        return dataAccessService;
    }
    
    /**
     * 获取缓存服务
     */
    public UnifiedCacheService getCacheService() {
        return cacheService;
    }
    
    /**
     * 检查是否处于开发环境
     */
    public boolean isDevelopment() {
        return environmentDetector.isDevelopment();
    }
    
    /**
     * 检查是否处于生产环境
     */
    public boolean isProduction() {
        return environmentDetector.isProduction();
    }
    
    /**
     * 获取当前环境信息
     */
    public String getEnvironmentInfo() {
        return String.format("环境: %s, 数据库: %s, 缓存: %s", 
            environmentDetector.getActiveProfile(),
            dataAccessService.getDatabaseType(),
            cacheService.getCacheType());
    }
    
    /**
     * 执行健康检查
     */
    public HealthCheckResult healthCheck() {
        HealthCheckResult result = new HealthCheckResult();
        
        try {
            // 检查数据访问服务
            result.setDataAccessHealthy(true);
            result.setDataAccessType(dataAccessService.getDatabaseType().getName());
            
            // 检查缓存服务
            result.setCacheHealthy(true);
            result.setCacheType(cacheService.getCacheType().getName());
            
            // 检查环境检测器
            result.setEnvironmentDetectorHealthy(true);
            result.setActiveProfile(environmentDetector.getActiveProfile());
            
            result.setOverallHealthy(true);
            result.setMessage("All components healthy");
            
        } catch (Exception e) {
            logger.error("健康检查失败", e);
            result.setOverallHealthy(false);
            result.setMessage("健康检查失败: " + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 健康检查结果
     */
    public static class HealthCheckResult {
        private boolean overallHealthy;
        private boolean dataAccessHealthy;
        private boolean cacheHealthy;
        private boolean environmentDetectorHealthy;
        private String dataAccessType;
        private String cacheType;
        private String activeProfile;
        private String message;
        
        // Getters and Setters
        public boolean isOverallHealthy() { return overallHealthy; }
        public void setOverallHealthy(boolean overallHealthy) { this.overallHealthy = overallHealthy; }
        
        public boolean isDataAccessHealthy() { return dataAccessHealthy; }
        public void setDataAccessHealthy(boolean dataAccessHealthy) { this.dataAccessHealthy = dataAccessHealthy; }
        
        public boolean isCacheHealthy() { return cacheHealthy; }
        public void setCacheHealthy(boolean cacheHealthy) { this.cacheHealthy = cacheHealthy; }
        
        public boolean isEnvironmentDetectorHealthy() { return environmentDetectorHealthy; }
        public void setEnvironmentDetectorHealthy(boolean environmentDetectorHealthy) { this.environmentDetectorHealthy = environmentDetectorHealthy; }
        
        public String getDataAccessType() { return dataAccessType; }
        public void setDataAccessType(String dataAccessType) { this.dataAccessType = dataAccessType; }
        
        public String getCacheType() { return cacheType; }
        public void setCacheType(String cacheType) { this.cacheType = cacheType; }
        
        public String getActiveProfile() { return activeProfile; }
        public void setActiveProfile(String activeProfile) { this.activeProfile = activeProfile; }
        
        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
        
        @Override
        public String toString() {
            return "HealthCheckResult{" +
                "overallHealthy=" + overallHealthy +
                ", dataAccessHealthy=" + dataAccessHealthy +
                ", cacheHealthy=" + cacheHealthy +
                ", environmentDetectorHealthy=" + environmentDetectorHealthy +
                ", dataAccessType='" + dataAccessType + '\'' +
                ", cacheType='" + cacheType + '\'' +
                ", activeProfile='" + activeProfile + '\'' +
                ", message='" + message + '\'' +
                '}';
        }
    }
}